package Presentacion;

import AccesoDatos.Conexion;
import AccesoDatos.Comando;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import java.sql.ResultSet;
import java.sql.SQLException;

public class frmLogin extends javax.swing.JFrame {
    Conexion conexion = new Conexion();

    public frmLogin() {
        initComponents();
    }
    
    @Override
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("src/imagenes/ingresar_icono.png"));
        return retValue;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbUsuario = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        btnIngresar = new javax.swing.JButton();
        lbContraseña = new javax.swing.JLabel();
        btnSalir = new javax.swing.JButton();
        lbBgLogin = new javax.swing.JLabel();
        txtContraseña = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ingreso al sistema - SICGSA");
        setBounds(new java.awt.Rectangle(0, 0, 500, 275));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImage(getIconImage());
        setName("frmLogin"); // NOI18N
        setPreferredSize(new java.awt.Dimension(500, 275));
        setResizable(false);
        getContentPane().setLayout(null);

        lbUsuario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbUsuario.setText("Usuario");
        getContentPane().add(lbUsuario);
        lbUsuario.setBounds(130, 40, 50, 30);

        txtUsuario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        getContentPane().add(txtUsuario);
        txtUsuario.setBounds(200, 40, 160, 30);

        btnIngresar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnIngresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/src/imagenes/ingresar_login.png"))); // NOI18N
        btnIngresar.setText("Ingresar");
        btnIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarActionPerformed(evt);
            }
        });
        getContentPane().add(btnIngresar);
        btnIngresar.setBounds(120, 140, 119, 41);

        lbContraseña.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbContraseña.setText("Contraseña");
        getContentPane().add(lbContraseña);
        lbContraseña.setBounds(110, 90, 70, 30);

        btnSalir.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/src/imagenes/salir_login.png"))); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        getContentPane().add(btnSalir);
        btnSalir.setBounds(260, 140, 100, 41);
        getContentPane().add(lbBgLogin);
        lbBgLogin.setBounds(0, 0, 0, 0);

        txtContraseña.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        getContentPane().add(txtContraseña);
        txtContraseña.setBounds(200, 90, 160, 30);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/src/imagenes/bg_login.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 500, 250);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarActionPerformed
        try{
            conexion.openConexion();
            Comando comando = new Comando("select Username, Password, Tipo from tb_usuario where Username = '"+ txtUsuario.getText() +"'");
            ResultSet result = comando.ejecutarSQL(conexion); 
            if(result.next()){
            if((txtUsuario.getText().equals(result.getString("Username")))&&(txtContraseña.getText().equals(result.getString("Password")))){
            frmMain fMain = new frmMain(conexion); 
            fMain.setVisible(true); 
            this.setVisible(false);
            }else{
             JOptionPane.showMessageDialog(null,"El nombre de usuario y/o contraseña no son correctos","Acceso Denegado", JOptionPane.ERROR_MESSAGE);
            }
            }else{
             JOptionPane.showMessageDialog(null,"El nombre de usuario y/o contraseña no son correctos","Acceso Denegado", JOptionPane.ERROR_MESSAGE);
            }
            //conexion.closeConexion();
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }       
    
    }//GEN-LAST:event_btnIngresarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        int i =JOptionPane.showConfirmDialog(this,"¿Realmente desea Salir?","Confirmar Salida",JOptionPane.YES_NO_OPTION);
        if(i==0){
            System.exit(0);
        }
    }//GEN-LAST:event_btnSalirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            new frmLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnIngresar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbBgLogin;
    private javax.swing.JLabel lbContraseña;
    private javax.swing.JLabel lbUsuario;
    private javax.swing.JPasswordField txtContraseña;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
